<?php

/**
 * Class BaseController
 */
class BaseController extends Controller{
    public static $default_db = "db_sys_26_71";
    public static $app_id;
    public static $api_key = 'kwinbon.grain';
    public static $global_configs;
    /**
     * 前函数
     */
    public function init($is_login = true){
        //Gpsclient::setGpsServerUrl(SITE_PATH);
        if($this->get('api_key')){
            $api_key = $this->get('api_key');
            session::set("pcorder_api_key",$api_key,28800*2);
        }else{
            $api_key = session:: get('pcorder_api_key');
        }
        
		self::$default_db = 'db_sys_26_71';
        $model = Controller::createMode(self::$default_db,'t_26_tongyongpeizhibiao');
        $model->setConfig(parent::getConfig(self::$default_db));
        self::$global_configs = $model->select();
        $this->assign('shop_app',$app);
        $this->assign('shop_global_configs',self::$global_configs);
        $this->assign('shop_api_key',self::$api_key);
        $this->assign('shop_site_path',SITE_PATH);
        $this->assign('shop_user_id',LoginHelper::getUserId());
    }

    /**
     * 单例模式实例化一个Model对象
     *
     * 单例模式实现化一个model对象. 初次实例化某Model后, 当第二次实例化时则直接调用初次实现化的结果(object)
     *
     * @access public
     * @param string $modelName 所要实例化的Modle名称
     * @return object 实例化后的对象
     */
    public static function model($modelName){
        self::init();
        // 参数判断
        $model = parent::model($modelName);
        $model->setConfig(parent::getConfig(self::$default_db));
        return $model;
    }
    public static function showMessage($message, $gotoUrl = null, $limitTime = 5){

        // 参数分析
        if (!$message){
            return false;
        }

        // 当自定义跳转网址存在时
        if (!is_null($gotoUrl)){
            $limitTime = 1000 * $limitTime;
            // 分析自定义网址是否为返回页
            if ($gotoUrl == -1){
                $gotoUrl = 'javascript:history.go(-1);';
                $message .= '<br/><a style="display: block;font-size: 1rem;width:90%;" href="javascript:history.go(-1);" target="_self">如果你的浏览器没反应,请点击这里...</a>';
            }else{
                // 防止网址过长，有换行引起跳转变不正确
                $gotoUrl = str_replace(array("\n", "\r"), '', $gotoUrl);
                $message .= '<br/><a style="display: block;font-size: 1rem;width:90%;" href="' . $gotoUrl . '" target="_self">如果你的浏览器没反应,请点击这里...</a>';
            }
            $message .= '<script type="text/javascript">function pdk_redirect_url(url){location.href=url;}setTimeout("pdk_redirect_url(\'' . $gotoUrl . '\')", ' . $limitTime . ');</script>';
        }
        $app_skin_path = APP_SKIN;
        $messageTemplateFile = VIEW_DIR . 'html/exception.php';
        is_file($messageTemplateFile) ? include_once $messageTemplateFile : include_once VIEW_DIR . 'html/message.php';
        exit();
    }
}
